package com.jack.lib.ui.widget

import android.content.Context
import android.widget.LinearLayout
import kotlin.jvm.JvmOverloads
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import com.jack.lib.ui.R
import android.view.ViewGroup

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/11/20 14:21
 */
open class JLinearLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private var mJRadio = false
    private var mJAutoPress = false
    private var mOnCopyClickListener: OnCopyClickListener? = null
    private var mOnClickListener: OnClickListener? = null
    private var mClickGap = 0
    private var mOldClickTime: Long = 0
    private var mOnChildSelectedListener: OnChildSelectedListener? = null

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.JLinearLayout)
        parseAttr(typedArray)
        typedArray.recycle()
    }

    private fun parseAttr(typedArray: TypedArray) {
        mJRadio = typedArray.getBoolean(R.styleable.JLinearLayout_JRadio, false)
        mJAutoPress = typedArray.getBoolean(R.styleable.JLinearLayout_JAutoPress, false)
        val select = typedArray.getBoolean(R.styleable.JLinearLayout_JSelect, false)
        mClickGap = typedArray.getInteger(R.styleable.JLinearLayout_JClickGap, 0)
        if (select) {
            isSelected = select
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        if (mJRadio) {
            radio()
        }
    }

    override fun addView(child: View, index: Int, params: ViewGroup.LayoutParams) {
        super.addView(child, index, params)
        if (mJRadio) {
            radio()
        }
    }

    override fun removeView(view: View) {
        super.removeView(view)
        if (mJRadio) {
            radio()
        }
    }

    override fun removeViewAt(index: Int) {
        super.removeViewAt(index)
        if (mJRadio) {
            radio()
        }
    }

    private fun radio() {
        for (i in 0 until childCount) {
            val view = getChildAt(i)
            view.setOnClickListener { v: View? ->
                for (j in 0 until childCount) {
                    val childAt = getChildAt(j)
                    if (childAt.isSelected != (i == j)) {
                        childAt.isSelected = i == j
                        if (mOnChildSelectedListener != null) {
                            mOnChildSelectedListener!!.setSelected(i == j, j, childAt)
                        }
                    }
                }
            }
        }
    }

    fun setOnChildSelectedListener(onChildSelectedListener: OnChildSelectedListener?) {
        mOnChildSelectedListener = onChildSelectedListener
    }

    interface OnChildSelectedListener {
        fun setSelected(selected: Boolean, index: Int, view: View?)
    }

    override fun setPressed(pressed: Boolean) {
        super.setPressed(pressed)
        val alpha = if (pressed && isClickable) 0.8f else 1f
        if (mJAutoPress && alpha != getAlpha()) {
            setAlpha(alpha)
        }
    }

    fun gone(gone: Boolean) {
        visibility = if (gone) GONE else VISIBLE
    }

    fun invisible(invisible: Boolean) {
        visibility = if (invisible) INVISIBLE else VISIBLE
    }

    fun gone() {
        visibility = GONE
    }

    fun invisible() {
        visibility = INVISIBLE
    }

    fun visible() {
        visibility = VISIBLE
    }

    override fun setOnClickListener(l: OnClickListener?) {
        super.setOnClickListener(l)
        mOnClickListener = l
    }

    override fun performClick(): Boolean {
        val millis = System.currentTimeMillis()
        if (millis - mOldClickTime < mClickGap) {
            return false
        }
        mOldClickTime = millis
        val click = super.performClick()
        if (mOnClickListener != null && mOnCopyClickListener != null) {
            mOnCopyClickListener!!.onClick(this)
        }
        return click
    }

    interface OnCopyClickListener {
        fun onClick(v: View?)
    }

    fun setOnCopyClickListener(onCopyClickListener: OnCopyClickListener?) {
        mOnCopyClickListener = onCopyClickListener
    }
}